package com.nick.sscplan.ui;

import java.util.ArrayList;
import java.util.List;



import android.annotation.SuppressLint;
import android.app.ActivityGroup;
import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

public class SscActivity extends ActivityGroup {
    
    private static final String TAG = "ssc_plan";

    private ViewPager mViewPager = null;
    private RadioGroup mSscTitle = null;
    private RadioButton mSscPlanHead = null, mSscHistoryHead = null, mSscChartHead = null;// 页卡头标
    private Context mContext = null;
    private View mSscPlan = null, mSscHistory = null, mSscChart = null;
    private List<View> listViews = null;// Tab页面列表
    private ImageView cursor = null;// 动画图片
    private int offset = 0;// 动画图片偏移量
    private int currIndex = 1;// 当前页卡编号
    private int bitmapwidth;// 动画图片宽度
    private int ooxx = 0;
    
    
    private static final int SSC_HISTORY = 0;
    private static final int SSC_PLAN = 1;
    private static final int SSC_CHART = 2;
    
    
    
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        initImageView();
        initTitle();
        initViewPager();
    }
    
    
    private void initTitle(){
        mSscPlanHead = (RadioButton) findViewById(R.id.ssc_plan_head);
        mSscHistoryHead = (RadioButton) findViewById(R.id.ssc_history_head);
        mSscChartHead = (RadioButton) findViewById(R.id.ssc_chart_head);
        mSscTitle = (RadioGroup) findViewById(R.id.ssc_title);
        
//        mSscPlanHead.setOnClickListener(new MyOnClickListener(SSC_PLAN));
//        mSscHistoryHead.setOnClickListener(new MyOnClickListener(SSC_HISTORY));
//        mSscChartHead.setOnClickListener(new MyOnClickListener(SSC_CHART));
        mSscTitle.setOnCheckedChangeListener(new MyOnCheckedChangeListener());
        
    }
    
    
    private void initViewPager(){
        mViewPager = (ViewPager) findViewById(R.id.ssc_main);
        listViews = new ArrayList<View>();
        mSscHistory = getLocalActivityManager().startActivity("ssc_history", new Intent(this, Ssc_HistoryActivity.class)).getDecorView();
        mSscPlan = getLocalActivityManager().startActivity("ssc_plan", new Intent(this, Ssc_PlanActivity.class)).getDecorView();
        mSscChart = getLocalActivityManager().startActivity("ssc_chart", new Intent(this, Ssc_ChartActivity.class)).getDecorView();
        listViews.add(mSscHistory);
        listViews.add(mSscPlan);
        listViews.add(mSscChart);
        mViewPager.setAdapter(new MyPagerAdapter(listViews));
        mViewPager.setCurrentItem(SSC_PLAN);
        mViewPager.setOnPageChangeListener(new MyOnPageChangeListener());
        
    }
    
    
    /**
     * 初始化动画
     */
    private void initImageView() {
        cursor = (ImageView) findViewById(R.id.cursor);
        bitmapwidth = BitmapFactory.decodeResource(getResources(), R.drawable.back2)
                .getWidth();// 获取图片宽度
        DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);
        int screenW = dm.widthPixels;// 获取分辨率宽度
        offset = (screenW / 3 - bitmapwidth) / 2;// 计算偏移量
        Log.d(TAG, "offset = " + offset);
        ooxx = screenW/2;
        Matrix matrix = new Matrix();
        matrix.postTranslate(offset, 0);
        cursor.setImageMatrix(matrix);// 设置动画初始位置
    }
    
    /**
     * Title on click Listener.
     * @author zhangxin
     *
     */
    private class MyOnClickListener implements View.OnClickListener{

        private int index = 0;

        public MyOnClickListener(int i) {
            index = i;
        }

        @Override
        public void onClick(View v) {
            mViewPager.setCurrentItem(index);
        }
        
    }
    
    private class MyOnCheckedChangeListener implements android.widget.RadioGroup.OnCheckedChangeListener{

        public void onCheckedChanged(RadioGroup group, int checkedId) {
            switch (checkedId) {
            case R.id.ssc_history_head:
                mViewPager.setCurrentItem(SSC_HISTORY);
                break;
            case R.id.ssc_plan_head:
                mViewPager.setCurrentItem(SSC_PLAN);
                break;
            case R.id.ssc_chart_head:
                mViewPager.setCurrentItem(SSC_CHART);
                break;

            default:
                break;
            }
        }
        
    }
    
    
    /**
     * 页卡切换监听
     */
    public class MyOnPageChangeListener implements OnPageChangeListener {

        int one = offset * 2  + bitmapwidth;// 页卡1 -> 页卡2 偏移量
        int two = one * 2;// 页卡1 -> 页卡3 偏移量
        int three = one * 3;
        int four = one * 4;

        @Override
        public void onPageSelected(int position) {
            Animation animation = null;
            switch (position) {
            case SSC_HISTORY:
                if (currIndex == SSC_PLAN) {
                    animation = new TranslateAnimation(one, 0, 0, 0);
                } else if (currIndex == SSC_CHART) {
                    animation = new TranslateAnimation(two, 0, 0, 0);
                }
                mSscHistoryHead.setChecked(true);
                break;
            case SSC_PLAN:
                if (currIndex == SSC_HISTORY) {
                    animation = new TranslateAnimation(offset, one, 0, 0);
                } else if (currIndex == SSC_CHART) {
                    animation = new TranslateAnimation(two, one, 0, 0);
                }
                mSscPlanHead.setChecked(true);
                break;
            case SSC_CHART:
                if (currIndex == SSC_HISTORY) {
                    animation = new TranslateAnimation(offset, two, 0, 0);
                } else if (currIndex == SSC_PLAN) {
                    animation = new TranslateAnimation(one, two, 0, 0);
                }
                mSscChartHead.setChecked(true);
                break;
            }
            currIndex = position;
            animation.setFillAfter(true);// True:图片停在动画结束位置
            animation.setDuration(300);
            Log.d(TAG, "offset = " + cursor.getImageMatrix());
            cursor.startAnimation(animation);
        }
            
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            public void onPageScrollStateChanged(int state) {
            }
        }
    
    
    
    
    
        /**
         * ViewPager适配器
         */
        public class MyPagerAdapter extends PagerAdapter {
            public List<View> mListViews;

            public MyPagerAdapter(List<View> mListViews) {
                this.mListViews = mListViews;
            }

            @Override
            public void destroyItem(View arg0, int arg1, Object arg2) {
                ((ViewPager) arg0).removeView(mListViews.get(arg1));
            }

            @Override
            public void finishUpdate(View arg0) {
            }

            @Override
            public int getCount() {
                return mListViews.size();
            }

            @Override
            public Object instantiateItem(View arg0, int arg1) {
                ((ViewPager) arg0).addView(mListViews.get(arg1), 0);
                return mListViews.get(arg1);
            }

            @Override
            public boolean isViewFromObject(View arg0, Object arg1) {
                return arg0 == (arg1);
            }

            @Override
            public void restoreState(Parcelable arg0, ClassLoader arg1) {
            }

            @Override
            public Parcelable saveState() {
                return null;
            }

            @Override
            public void startUpdate(View arg0) {
            }
        }
    
}
